<template>
    <div class="game-card-talk-list" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="请输入关键字"
                clearable
                @keyup.enter.native="clearGetList"
                style="width:200px;"
                v-model.trim="content">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>

            <span class="input-title">评论类型</span>
            <el-select
                style="margin-left:10px;width:150px"
                @change="clearGetList"
                v-model="commentLevel">
                <el-option label="全部" :value="0"></el-option>
                <el-option label="一级评论" :value="1"></el-option>
                <el-option label="二级评论" :value="2"></el-option>
            </el-select>

            <span class="input-title">状态</span>
            <el-select
                style="margin-left:10px;width:150px"
                @change="clearGetList"
                v-model="status">
                <el-option label="全部" :value="-3"></el-option>
                <el-option label="显示" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
                <el-option label="系统删除" :value="-2"></el-option>
                <el-option label="用户删除" :value="-1"></el-option>
            </el-select>

            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
        </div>
        <div class="table-box">
            <el-table
                ref="table"
                @sort-change="sortChange"
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                    fixed
                    prop="id"
                    label="ID">
                </el-table-column>

                <el-table-column
                    min-width="400"
                    prop="content"
                    label="评论内容">
                </el-table-column>
                    <!-- prop="content" -->
                <el-table-column
                    min-width="100"
                    prop="lastCommentContent"
                    label="游戏单">
                    <template slot-scope="scope">
                        <div class="game-card" v-if="scope.row.userGameListVo">
                            <img :src="scope.row.userGameListVo.cover" alt="">
                            <div class="table-btn" @click="goGameCardDetail(scope.row.listid)">{{scope.row.userGameListVo.title}}</div>
                        </div>
                    </template>
                </el-table-column>

                <el-table-column
                    prop="thumbsnum"
                    sortable="custom"
                    width="100"
                    label="点赞数">
                    <template slot-scope="scope">
                        <div>{{scope.row.thumbsnum || 0}}</div>
                    </template>
                </el-table-column>

                <!-- <el-table-column
                    prop="gameTopic"
                    sortable="custom"
                    width="100"
                    label="举报数">
                </el-table-column> -->

                <el-table-column
                    prop="replynum"
                    sortable="custom"
                    width="100"
                    label="回复数">
                    <template slot-scope="scope">
                        <div>{{scope.row.replynum || 0}}</div>
                    </template>
                </el-table-column>

                <el-table-column
                    min-width="120"
                    prop="userName"
                    label="评论者">
                    <template slot-scope="scope">
                        <div class="table-btn" @click="goUserDetail(scope.row.userid)">{{scope.row.userName || scope.row.userid}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    label="评论时间">
                    <template slot-scope="scope">
                        <div style="text-align:center;">{{scope.row.createtime | timeInit}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="评论类型">
                    <template slot-scope="scope">
                        <div v-if="scope.row.replyid" style="opacity: .5">二级评论</div>
                        <div v-else>一级评论</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    fixed="right"
                    label="状态">
                    <template slot-scope="scope">
                        <div style="color: #F56C6C;" v-if="scope.row.status === -2">系统删除</div>
                        <div v-else-if="scope.row.status === -1">用户删除</div>
                        <div style="color: #F56C6C;" v-else-if="scope.row.status === 0">
                            <div class="point red-point"></div>
                            <span>已禁用</span>
                        </div>
                        <div style="color:#67C23A;" v-else-if="scope.row.status === 1">
                            <span class="point"></span>
                            <span>显示中</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    label="操作">
                    <template slot-scope="scope">
                        <!-- -2系统删除-1用户删除0未显示1显示中 -->
                        <div v-if="scope.row.status === 1"><el-button @click="changeGameTalkStatus(scope.row, 0)" type="text" size="small" style="color:#F56C6C;">禁用</el-button></div>
                        <div v-else-if="scope.row.status === 0"><el-button @click="changeGameTalkStatus(scope.row, 1)" type="text" size="small" style="color:#67C23A;">启用</el-button></div>
                        <div><el-button @click="goCommentDetail(scope.row.id)" type="text" size="small">查看详情</el-button></div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            commentLevel: 0,
            content: '',
            status: -3, // -2系统删除-1用户删除0未显示1显示中
            type: '',
            sort: '',
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false
        }
    },
    activated () {
        // 更好的缓存数据
        this.getList(this.pageNum)
    },
    methods: {
        // 获取点评列表
        async getList (pageNum = 1) {
            if (this.loading) return
            this.loading = true
            this.pageNum = pageNum
            let params = {
                pageNum: pageNum,
                pageSize: this.pageSize
            }
            if (this.content) {
                params.content = this.content
            }
            if (this.type) {
                params.type = this.type
            }
            if (this.sort) {
                params.sort = this.sort
            }
            params.status = this.status >= -2 ? this.status : null
            params.commentLevel = this.commentLevel > 0 ? this.commentLevel : null
            let res = await this.$http.get('/gameList/selectComment', {
                params: params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 初始化状态
        clearGetList () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },

        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                this.type = prop
            }
            this.getList(1)
        },

        // 页容切换
        sizeChange (num) {
            this.pageSize = num
            this.getList(1)
        },

        // 页码切换
        pageChange (num) {
            this.pageNum = num
            this.getList(this.pageNum)
        },

        // 更改评论状态
        async changeGameTalkStatus (item, status) {
            // 状态:0禁用 1启用
            let params = {
                id: item.id,
                status
            }
            let res = await this.$http.get('/gameList/updateCommentStatus', {
                params
            })
            if (res.code === '0') {
                item.status = status
                this.$message({
                    type: 'success',
                    message: '修改成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },

        // 查看评论详情
        goCommentDetail (id) {
            if (id) {
                this.$router.push({
                    path: '/gameCard/talkDetail',
                    query: {
                        id
                    }
                })
            } else {
                this.$router.push('/gameCard/talkDetail')
            }
        },

        // 查看游戏单详情
        goGameCardDetail (id) {
            const { href } = this.$router.resolve({
                path: '/gameCard/detail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 差看用户详情
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        }
    },

    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD HH:MM:SS')
            } else {
                return '-'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .game-card-talk-list {
        .input-box {
            .btn {
                margin-left: 10px;
            }
            .input-title {
                font-size: 13px;
                font-weight: 500;
                margin-left: 20px;
            }
        }
        .table-box {
            margin: 20px 0;
            .game-card {
                > img {
                    width: 80px;
                    height: 80px;
                    margin-bottom: 10px;
                }
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }

        .point {
            display:inline-block;
            width:10px;
            height:10px;
            border-radius: 50%;
            background-color:#67C23A;
            margin-right:3px;
        }
        .red-point {
            background-color: #F56C6C;
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
